package com.apache.passport.common;

/**
 * description:  加解密算法,此类主要用于对cookie进行加密的,故没有添加对中文的解析
 * @author Hou Dayu 创建时间：2014-12-31  
 * @Copyright(c)2014:
 */
public class Encipher {

	private static Encipher instance;

	private Encipher() {
	}

	public static Encipher getInstane() {
		if (instance == null) {
			instance = new Encipher();
		}

		return instance;
	}

	/**
	 * description:  加密,使用伪算法,并添加des,asc加密
	 * @param strPasswd
	 * @return  
	 * @author Hou Dayu 
	 * @update 2014-12-31
	 */
	public String encodeCookie(String cookie) {
		String strEncodePasswd = new String("");
		int n;
		char code;
		String des = new String();
		String strKey = new String();
		if ((cookie == null) || cookie.length() == 0) {
			strEncodePasswd = "";
			return strEncodePasswd;
		}
		strKey = EncipherConst.m_strKey1 + EncipherConst.m_strKey2 + EncipherConst.m_strKey3 + EncipherConst.m_strKey4
				+ EncipherConst.m_strKey5 + EncipherConst.m_strKey6;
		while (cookie.length() < 8) {
			cookie = cookie + (char) 1;
		}
		des = "";
		for (n = 0; n <= cookie.length() - 1; n++) {
			while (true) {
				code = (char) Math.rint(Math.random() * 100);
				while ((code > 0) && (((code ^ cookie.charAt(n)) < 0) || ((code ^ cookie.charAt(n)) > 90))) {
					code = (char) ((int) code - 1);
				}
				char mid = 0;
				int flag;
				flag = code ^ cookie.charAt(n);
				if (flag > 93) {
					mid = 0;
				} else {
					mid = strKey.charAt(flag);
				}
				if ((code > 35) & (code < 122) & (code != 124) & (code != 39) & (code != 44) & (mid != 124)
						& (mid != 39) & (mid != 44)) {
					break;
				}
			}
			char temp = 0;
			temp = strKey.charAt(code ^ cookie.charAt(n));
			des = des + (char) code + temp;
		}
		strEncodePasswd = des;
		return strEncodePasswd;
	}

	/**
	 * description:解密,伪密码解密  
	 * @param varCode
	 * @return  
	 * @author Hou Dayu 
	 * @update 2014-12-31
	 */
	public String decodeCookie(String cookie) {
		int n;
		String des = new String();
		String strKey = new String();
		if ((cookie == null) || (cookie.length() == 0)) {
			return "";
		}
		strKey = EncipherConst.m_strKey1 + EncipherConst.m_strKey2 + EncipherConst.m_strKey3 + EncipherConst.m_strKey4
				+ EncipherConst.m_strKey5 + EncipherConst.m_strKey6;
		if (cookie.length() % 2 == 1) {
			cookie = cookie + "?";
		}
		des = "";
		for (n = 0; n <= cookie.length() / 2 - 1; n++) {
			char b;
			b = cookie.charAt(n * 2);
			int a;
			a = (int) strKey.indexOf(cookie.charAt(n * 2 + 1));
			des = des + (char) ((int) b ^ a);
		}
		n = des.indexOf(1);
		if (n > 0) {
			return des.substring(0, n);
		} else {
			return des;
		}
	}

	static class EncipherConst {
		public final static String m_strKey1 = "zxcvbnm,./asdfg";
		public final static String m_strKeya = "cjk;";
		public final static String m_strKey2 = "hjkl;'qwertyuiop";
		public final static String m_strKeyb = "cai2";
		public final static String m_strKey3 = "[]\\1234567890-";
		public final static String m_strKeyc = "%^@#";
		public final static String m_strKey4 = "=` ZXCVBNM<>?:LKJ";
		public final static String m_strKeyd = "*(N";
		public final static String m_strKey5 = "HGFDSAQWERTYUI";
		public final static String m_strKeye = "%^HJ";
		public final static String m_strKey6 = "OP{}|+_)(*&^%$#@!~";
	}

	public static void main(String[] args) throws Exception {

		String a = Encipher.getInstane().encodeCookie("cuiyongxu1028@163.com");
		System.out.println(a); // 输出加密后的字符串
		System.out.println(Encipher.getInstane().decodeCookie(a)); // 把加密字符串解密后输出
	}
}